package com.by_man.presenter.menu_activity;

import android.app.AlertDialog;
import android.app.DatePickerDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.text.InputType;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;

import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;

import com.by_man.presenter.LoginActivity;
import com.by_man.presenter.R;
import com.by_man.presenter.SharedApplication;
import com.by_man.presenter.custom_layout.MultiSpinner;
import com.by_man.presenter.custom_layout.SimpleSpinnerOption;
import com.by_man.presenter.main_page.MainActivity;
import com.by_man.repository.LocalDataManager;
import com.by_man.repository.NetworkDataManager;
import com.by_man.repository.UserInfos;
import com.google.gson.Gson;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class Setting_self extends AppCompatActivity {
    private Handler handler;
    private ProgressDialog progressDialog;
    private boolean isContinue = false;

    private Toolbar mToolbar;
    private Button save;
    private EditText setting_name;
    private EditText setting_sex;
    private EditText setting_hight;
    private EditText setting_weight;
    private TextView setting_brith;
    private EditText setting_age;
    private EditText setting_telephone;
    private EditText setting_xuexing;
    private EditText setting_xiyan;
    private EditText setting_yinjiu;
    private MultiSpinner setting_jiwangbing;
    private EditText setting_guomin;
    private MultiSpinner setting_jiazu;
    private int year,year1;
    private int month1;
    private int day1;
    private ScrollView scrollselfInfo;

    private PopupWindow yesorno_xiyan;
    private PopupWindow yesorno_yinjiu;
    private PopupWindow yesorno_sex;
    private PopupWindow yesorno_xuexing;
    private PopupWindow yesorno_guomin;

    private LinearLayout ll_yesorno_xiyan;
    private LinearLayout ll_yesorno_yinjiu;
    private LinearLayout ll_yesorno_sex;
    private LinearLayout ll_yesorno_xuexing;
    private LinearLayout ll_yesorno_guomin;

    private LinearLayout ll_xiyan;
    private LinearLayout ll_yinjiu;
    private LinearLayout ll_sex;
    private LinearLayout ll_xuexing;
    private LinearLayout ll_guomin;
    private LinearLayout ll_jiazhu;
    private LinearLayout ll_jiwang;
    private int editxnum = 0;
    private static final String[] h={"传染病史","遗传病史","心脑血管疾病史","无"};
    private static final String[] jw={"高血压 ","糖尿病 ","肝胆疾病史 ","肾脏疾病史 ","心脏疾病史 ","脑血管疾病史 ","手术史 ","输血史 ","外伤史 ","无 "};



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting_self);
        handler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                if (msg.what == 125) {      // 证明为更新信息后返回
                    boolean isFirst = getIntent().getBooleanExtra("isFirst", false);
                    if (isFirst) {          // 如果是第一次设置信息，跳转主界面
                        Intent intent = new Intent(Setting_self.this, MainActivity.class);
                        startActivity(intent);
                    } else { // 留在页面
                        //Toast.makeText(Setting_self.this, "更新信息成功！", Toast.LENGTH_SHORT);
                    }
                } else {                // 不是更新信息返回，直接更新ui界面
                    initView();
                }
            }
        };
        dataInitWait();
    }


    //没有这句话menu不显示
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.setting_edittext, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
//                boolean isFirst = getIntent().getBooleanExtra("isFirst", false);
//                if (!isFirst) {
//                    Intent intent = new Intent(this, SettingActivity.class);
//                    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
//                    startActivity(intent);
//                } else {
//                    Intent intent = new Intent(this, LoginActivity.class);
//                    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
//                    startActivity(intent);
//                }
                onBackPressed();
                break;
            case R.id.menu_setting:
                setting_name.setEnabled(true);
                setting_sex.setEnabled(true);
                setting_hight.setEnabled(true);
                setting_weight.setEnabled(true);
                setting_brith.setEnabled(true);
                setting_age.setEnabled(true);
                setting_telephone.setEnabled(true);
                setting_xuexing.setEnabled(true);
                setting_xiyan.setEnabled(true);
                setting_yinjiu.setEnabled(true);
                setting_jiwangbing.setEnabled(true);
                setting_guomin.setEnabled(true);
                setting_jiazu.setEnabled(true);
                setting_brith.setFocusable(true);
                ll_xiyan.setClickable(true);
                ll_yinjiu.setClickable(true);
                ll_sex.setClickable(true);
                ll_xuexing.setClickable(true);
                ll_guomin.setClickable(true);
                ll_jiazhu.setClickable(true);
                ll_jiwang.setClickable(true);
                setting_jiazu.setClickable(true);
                setting_jiwangbing.setClickable(true);
            default:
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    private void initView() {
        mToolbar = (Toolbar) this.findViewById(R.id.tool_bar);
        //初始化ToolBar
        mToolbar.setTitle("个人信息修改");
        setSupportActionBar(mToolbar);
        ActionBar actionBar = getSupportActionBar();
        actionBar.setHomeAsUpIndicator(R.drawable.icon_back);
        actionBar.setDisplayHomeAsUpEnabled(true);


        scrollselfInfo= (ScrollView) findViewById(R.id.scrollself);
        setting_name=(EditText)findViewById(R.id.setting_name);
        setting_age=(EditText)findViewById(R.id.setting_age);
        setting_weight=(EditText)findViewById(R.id.setting_weight);
        setting_hight=(EditText)findViewById(R.id.setting_height);
        setting_telephone= (EditText) findViewById(R.id.setting_telephone);
        setting_xiyan=(EditText)findViewById(R.id.setting_xiyan);
        setting_xuexing=(EditText)findViewById(R.id.setting_xuexing);
        setting_jiwangbing=(MultiSpinner)findViewById(R.id.setting_jiwangbing);
        setting_guomin=(EditText)findViewById(R.id.setting_guomin);
        setting_yinjiu=(EditText)findViewById(R.id.setting_yinjiu);
        setting_sex=(EditText)findViewById(R.id.setting_sex);
        setting_brith=(TextView)findViewById(R.id.setting_brith);
        setting_jiazu=(MultiSpinner) findViewById(R.id.setting_jiazu);
        save=(Button)findViewById(R.id.save);



        ArrayList spinnerList1=new ArrayList();
        for(int i=0;i<10;i++){
            SimpleSpinnerOption option=new SimpleSpinnerOption();
            option.setName(jw[i]);
            option.setValue(i);
            spinnerList1.add(option);
        }
        setting_jiwangbing.setDataList(spinnerList1);

        ArrayList spinnerList2=new ArrayList();
        for(int i=0;i<4;i++){
            SimpleSpinnerOption option=new SimpleSpinnerOption();
            option.setName(h[i]);
            option.setValue(i);
            spinnerList2.add(option);
        }
        setting_jiazu.setDataList(spinnerList2);

        initPopuWindow();

        ll_xiyan = (LinearLayout)findViewById(R.id.setting_linear_xiyan);
        ll_yinjiu = (LinearLayout)findViewById(R.id.setting_linear_yinjiu);
        ll_sex = (LinearLayout)findViewById(R.id.setting_linear_sex);
        ll_xuexing = (LinearLayout)findViewById(R.id.setting_linear_xuexing);
        ll_guomin = (LinearLayout)findViewById(R.id.setting_linear_guomin);
        ll_jiazhu = (LinearLayout)findViewById(R.id.setting_linear_jiazhu);
        ll_jiwang = (LinearLayout)findViewById(R.id.setting_linear_jiwang);


        setting_xiyan.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                yesorno_xiyan.showAtLocation(getLayoutInflater().inflate(R.layout.activity_setting_self, null), Gravity.BOTTOM, 0, 0);
            }
        });


        setting_yinjiu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                yesorno_yinjiu.showAtLocation(getLayoutInflater().inflate(R.layout.activity_setting_self, null), Gravity.BOTTOM, 0, 0);

            }
        });

        setting_sex.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                yesorno_sex.showAtLocation(getLayoutInflater().inflate(R.layout.activity_setting_self, null), Gravity.BOTTOM, 0, 0);
            }
        });

        setting_xuexing.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                yesorno_xuexing.showAtLocation(getLayoutInflater().inflate(R.layout.activity_setting_self, null), Gravity.BOTTOM, 0, 0);
            }
        });

//        setting_guomin.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                yesorno_guomin.showAtLocation(getLayoutInflater().inflate(R.layout.activity_setting_self, null), Gravity.BOTTOM, 0, 0);
//            }
//        });

        Calendar c = Calendar.getInstance();
        year = c.get(Calendar.YEAR);
        //setting_name.setFocusableInTouchMode(false);
        setting_brith.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DatePickerDialog dpd = new DatePickerDialog(Setting_self.this, AlertDialog.THEME_HOLO_LIGHT,Datelistener, 1990, 0, 1);
                dpd.show();//显示DatePickerDialog组件

            }

        });
        //setting_name.setFocusableInTouchMode(false);
        setting_brith.setInputType(InputType.TYPE_DATETIME_VARIATION_NORMAL);


        //请求数据
        JSONArray jsonArray = UserInfos.Self_Infos;
        if (jsonArray != null) {
            try {
                for (int j = 0; j < jsonArray.length(); j++) {
                    JSONObject temp = (JSONObject) jsonArray.get(j);
                    final String userid = temp.getString("list_id");
                    int i = Integer.valueOf(userid).intValue();

                    switch (i) {
                        case 1:
                            setting_hight.setText(temp.getString("value"));
                            break;
                        case 2:
                            setting_weight.setText(temp.getString("value"));
                            break;
                        case 3:
                            setting_xuexing.setText(temp.getString("value"));
                            break;
                        case 4:
                            setting_sex.setText(temp.getString("value"));
                            break;
                        case 5:
                            setting_jiazu.setText(temp.getString("value"));
                            break;
                        case 6:
                            setting_telephone.setText(temp.getString("value"));
                            break;
                        case 7:
                            setting_age.setText(temp.getString("value"));
                            break;
                        case 8:
                            setting_jiwangbing.setText(temp.getString("value"));
                            break;
                        case 9:
                            setting_xiyan.setText(temp.getString("value"));
                            break;
                        case 10:
                            setting_yinjiu.setText(temp.getString("value"));
                            break;
                        case 11:
                            setting_name.setText(temp.getString("value"));
                            break;
                        case 12:
                            setting_brith.setText(temp.getString("value"));
                            break;
                        case 20:
                            setting_guomin.setText(temp.getString("value"));
                            break;
                        default:
                            break;
                    }
                }
            } catch (JSONException je) {
                je.printStackTrace();
            }
        }

        save.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final String name = setting_name.getText().toString().trim();
                final String age = setting_age.getText().toString().trim();
                final String brith = setting_brith.getText().toString().trim();
                final String sex = setting_sex.getText().toString().trim();
                final String height = setting_hight.getText().toString().trim();
                final String weight = setting_weight.getText().toString().trim();
                final String telephone = setting_telephone.getText().toString().trim();
                final String jiwangbing = setting_jiwangbing.getText().toString().trim();
                final String jiazushi = setting_jiazu.getText().toString().trim();
                final String xuexing = setting_xuexing.getText().toString().trim();
                final String xiyan = setting_xiyan.getText().toString().trim();
                final String yinjiu = setting_yinjiu.getText().toString().trim();
                final String guomin = setting_guomin.getText().toString().trim();


                List<Map<String, Object>> testlist = new ArrayList<Map<String, Object>>();
                Map<String, Object> test = new HashMap<String, Object>();
                test.put("id", 1);
                test.put("value", height);
                testlist.add(test);
                Map<String, Object> test1 = new HashMap<String, Object>();
                test1.put("id", 2);
                test1.put("value", weight);
                testlist.add(test1);
                Map<String, Object> test2 = new HashMap<String, Object>();
                test2.put("id", 3);
                test2.put("value", xuexing);
                testlist.add(test2);
                Map<String, Object> test3 = new HashMap<String, Object>();
                test3.put("id", 4);
                test3.put("value", sex);
                testlist.add(test3);
                Map<String, Object> test4 = new HashMap<String, Object>();
                test4.put("id", 5);
                test4.put("value", jiazushi);
                testlist.add(test4);
                Map<String, Object> test5 = new HashMap<String, Object>();
                test5.put("id", 6);
                test5.put("value", telephone);
                testlist.add(test5);
                Map<String, Object> test6 = new HashMap<String, Object>();
                test6.put("id", 7);
                test6.put("value", age);
                testlist.add(test6);
                Map<String, Object> test7 = new HashMap<String, Object>();
                test7.put("id", 8);
                test7.put("value", jiwangbing);
                testlist.add(test7);
                Map<String, Object> test8 = new HashMap<String, Object>();
                test8.put("id", 9);
                test8.put("value", xiyan);
                testlist.add(test8);
                Map<String, Object> test9 = new HashMap<String, Object>();
                test9.put("id", 10);
                test9.put("value", yinjiu);
                testlist.add(test9);
                Map<String, Object> test10 = new HashMap<String, Object>();
                test10.put("id", 11);
                test10.put("value", name);
                testlist.add(test10);
                Map<String, Object> test11 = new HashMap<String, Object>();
                test11.put("id", 12);
                test11.put("value", brith);
                testlist.add(test11);
                Map<String, Object> test20 = new HashMap<String, Object>();
                test20.put("id", 20);
                test20.put("value", guomin);
                testlist.add(test20);

                Gson gson = new Gson();
                String dataString = gson.toJson(testlist).toString();
                LocalDataManager.updateSelfInfos(dataString, handler);
            }

        });
    }



    private DatePickerDialog.OnDateSetListener Datelistener=new DatePickerDialog.OnDateSetListener()
    {
        /**params：view：该事件关联的组件
         * params：myyear：当前选择的年
         * params：monthOfYear：当前选择的月
         * params：dayOfMonth：当前选择的日
         */
        @Override
        public void onDateSet(DatePicker view, int myyear, int monthOfYear, int dayOfMonth) {
            //修改year、month、day的变量值，以便以后单击按钮时，DatePickerDialog上显示上一次修改后的值
            year1=myyear;
            month1=monthOfYear;
            day1=dayOfMonth;
            //更新日期
            updateDate();

        }
        //当DatePickerDialog关闭时，更新日期显示
        private void updateDate()
        {
            //在TextView上显示日期
            if(year-year1<=0) {

                Toast.makeText(Setting_self.this,"请选择正确的日期",Toast.LENGTH_SHORT).show();

            }else {

            setting_brith.setText(year1+"-"+(month1+1)+"-"+day1);
            //Date mydate=new Date(); //获取当前日期Date对象;
            setting_age.setText((year-year1)+"岁");}

        }
    };
//    protected void onRestart()
//    {
//        super.onRestart();
////        Intent intent=new Intent(this,PassWord.class);
////        intent.putExtra("extra", this.getClass().getName());
////        startActivity(intent);
//    }

//    public boolean onKeyDown(int keyCode, KeyEvent event) {
//        if(keyCode == KeyEvent.KEYCODE_BACK) { //监控/拦截/屏蔽返回键
//            boolean isFirst = getIntent().getBooleanExtra("isFirst", false);
//            if (!isFirst) {
//                Intent intent = new Intent(Setting_self.this, SettingActivity.class);
//                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
//                startActivity(intent);
//            }
//        }
//        return super.onKeyDown(keyCode, event);
//    }

    protected void onDestroy() {
        super.onDestroy();
        handler.removeCallbacksAndMessages(null);
    }

    // 多次请求服务器后仍无反应，询问用户是否继续
    public void showRequestAgain() {
        AlertDialog alertDialog = new AlertDialog.Builder(this).setTitle("提醒")
                .setMessage("已多次请求服务器，是否继续请求？")
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        NetworkDataManager.DocNum = 3;
                        LocalDataManager.getAllDoctor();
                    }
                }).setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        isContinue = true;
                    }
                }).create();
        alertDialog.show();
    }

    private void dataInitWait() {
        if (null == UserInfos.Self_Infos) {     // 没有数据
            progressDialog = new ProgressDialog(this);
            progressDialog.setTitle("提示信息");
            progressDialog.setMessage("正在请求数据，请稍后......");
            progressDialog.setCancelable(false);
            progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
            progressDialog.show();

            if (NetworkDataManager.SelfInfosNum < 1) {  // 多次请求没有结果
                showRequestAgain();               // 询问用户是否继续请求数据
            }

            Thread thread = new Thread() {
                public void run() {
                    while (null == UserInfos.Self_Infos) {  // 阻塞等待数据的初始化
                        if (NetworkDataManager.SelfInfosNum < 1) {
                            showRequestAgain();
                        }
                        if (isContinue) break;
                    }
                    progressDialog.dismiss();
                    Message message = Message.obtain(handler);
                    message.sendToTarget();
                }
            };
            thread.setDaemon(true);             // 设置为守护线程
            thread.start();
        } else {
            initView();
        }
    }

    private void initPopuWindow() {
        // 吸烟史
        yesorno_xiyan = new PopupWindow(Setting_self.this);
        View view_xiyan = getLayoutInflater().inflate(R.layout.item_yesorno, null);
        ll_yesorno_xiyan = (LinearLayout) view_xiyan.findViewById(R.id.ll_yesorno);

        yesorno_xiyan.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        yesorno_xiyan.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        yesorno_xiyan.setBackgroundDrawable(new BitmapDrawable());
        yesorno_xiyan.setFocusable(true);
        yesorno_xiyan.setOutsideTouchable(true);
        yesorno_xiyan.setContentView(view_xiyan);

        RelativeLayout parent_xiyan = (RelativeLayout) view_xiyan.findViewById(R.id.parent);
        final Button xiyanbt0 = (Button) view_xiyan
                .findViewById(R.id.item_yesorno_mid);
        final Button xiyanbt1 = (Button) view_xiyan
                .findViewById(R.id.item_yesorno_yes);
        final Button xiyanbt2 = (Button) view_xiyan
                .findViewById(R.id.item_yesorno_no);
        final Button xiyanbt3 = (Button) view_xiyan
                .findViewById(R.id.item_yesorno_cancle);
        parent_xiyan.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                yesorno_xiyan.dismiss();
                ll_yesorno_xiyan.clearAnimation();
                xiyanbt0.setVisibility(View.GONE);
            }
        });

        xiyanbt1.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                setting_xiyan.setText("是");
                yesorno_xiyan.dismiss();
                ll_yesorno_xiyan.clearAnimation();
            }
        });
        xiyanbt2.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                setting_xiyan.setText("否");
                yesorno_xiyan.dismiss();
                ll_yesorno_xiyan.clearAnimation();
            }
        });
        xiyanbt3.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                yesorno_xiyan.dismiss();
                ll_yesorno_xiyan.clearAnimation();
            }
        });
        // 饮酒史
        yesorno_yinjiu = new PopupWindow(Setting_self.this);
        View view_yinjiu = getLayoutInflater().inflate(R.layout.item_yesorno, null);
        ll_yesorno_yinjiu = (LinearLayout) view_yinjiu.findViewById(R.id.ll_yesorno);

        yesorno_yinjiu.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        yesorno_yinjiu.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        yesorno_yinjiu.setBackgroundDrawable(new BitmapDrawable());
        yesorno_yinjiu.setFocusable(true);
        yesorno_yinjiu.setOutsideTouchable(true);
        yesorno_yinjiu.setContentView(view_yinjiu);

        RelativeLayout parent_yinjiu = (RelativeLayout) view_yinjiu.findViewById(R.id.parent);
        final Button yinjiubt0 = (Button) view_yinjiu
                .findViewById(R.id.item_yesorno_mid);
        final Button yinjiubt1 = (Button) view_yinjiu
                .findViewById(R.id.item_yesorno_yes);
        final Button yinjiubt2 = (Button) view_yinjiu
                .findViewById(R.id.item_yesorno_no);
        final Button yinjiubt3 = (Button) view_yinjiu
                .findViewById(R.id.item_yesorno_cancle);
        parent_yinjiu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                yesorno_yinjiu.dismiss();
                ll_yesorno_yinjiu.clearAnimation();
                yinjiubt0.setVisibility(View.GONE);
            }
        });

        yinjiubt1.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                setting_yinjiu.setText("是");
                yesorno_yinjiu.dismiss();
                ll_yesorno_yinjiu.clearAnimation();
            }
        });
        yinjiubt2.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                setting_yinjiu.setText("否");
                yesorno_yinjiu.dismiss();
                ll_yesorno_yinjiu.clearAnimation();
            }
        });
        yinjiubt3.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                yesorno_yinjiu.dismiss();
                ll_yesorno_yinjiu.clearAnimation();
            }
        });
        // 性别
        yesorno_sex = new PopupWindow(Setting_self.this);
        View view_sex = getLayoutInflater().inflate(R.layout.item_yesorno, null);
        ll_yesorno_sex = (LinearLayout) view_sex.findViewById(R.id.ll_yesorno);

        yesorno_sex.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        yesorno_sex.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        yesorno_sex.setBackgroundDrawable(new BitmapDrawable());
        yesorno_sex.setFocusable(true);
        yesorno_sex.setOutsideTouchable(true);
        yesorno_sex.setContentView(view_sex);

        RelativeLayout parent_sex = (RelativeLayout) view_sex.findViewById(R.id.parent);
        final Button sexbt0 = (Button) view_sex
                .findViewById(R.id.item_yesorno_mid);
        final Button sexbt1 = (Button) view_sex
                .findViewById(R.id.item_yesorno_yes);
        sexbt1.setText("男");
        final Button sexbt2 = (Button) view_sex
                .findViewById(R.id.item_yesorno_no);
        sexbt2.setText("女");
        final Button sexbt3 = (Button) view_sex
                .findViewById(R.id.item_yesorno_cancle);
        parent_sex.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                yesorno_sex.dismiss();
                ll_yesorno_sex.clearAnimation();
                sexbt0.setVisibility(View.GONE);
            }
        });

        sexbt1.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                setting_sex.setText("男");
                yesorno_sex.dismiss();
                ll_yesorno_sex.clearAnimation();
            }
        });
        sexbt2.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                setting_sex.setText("女");
                yesorno_sex.dismiss();
                ll_yesorno_sex.clearAnimation();
            }
        });
        sexbt3.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                yesorno_sex.dismiss();
                ll_yesorno_sex.clearAnimation();
            }
        });

        // 血型
        yesorno_xuexing = new PopupWindow(Setting_self.this);
        View view_xuexing  = getLayoutInflater().inflate(R.layout.item_yesorno, null);
        ll_yesorno_xuexing  = (LinearLayout) view_xuexing.findViewById(R.id.ll_yesorno);

        yesorno_xuexing .setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        yesorno_xuexing .setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        yesorno_xuexing .setBackgroundDrawable(new BitmapDrawable());
        yesorno_xuexing .setFocusable(true);
        yesorno_xuexing .setOutsideTouchable(true);
        yesorno_xuexing .setContentView(view_xuexing);

        RelativeLayout parent_xuexing  = (RelativeLayout) view_xuexing.findViewById(R.id.parent);
        final Button xuexingbt0 = (Button) view_xuexing
                .findViewById(R.id.item_yesorno_mid);
        xuexingbt0.setText("A");
        xuexingbt0.setVisibility(View.VISIBLE);
        final Button xuexingbt1 = (Button) view_xuexing
                .findViewById(R.id.item_yesorno_yes);
        xuexingbt1.setText("B");
        final Button xuexingbt2 = (Button) view_xuexing
                .findViewById(R.id.item_yesorno_no);
        xuexingbt2.setText("AB");
        final Button xuexingbt3 = (Button) view_xuexing
                .findViewById(R.id.item_yesorno_cancle);
        xuexingbt3.setText("O");
        parent_xuexing.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                yesorno_xuexing.dismiss();
                ll_yesorno_xuexing.clearAnimation();
            }
        });

        xuexingbt0.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                setting_xuexing.setText("A");
                yesorno_xuexing.dismiss();
                ll_yesorno_xuexing.clearAnimation();
            }
        });

        xuexingbt1.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                setting_xuexing.setText("B");
                yesorno_xuexing.dismiss();
                ll_yesorno_xuexing.clearAnimation();
            }
        });
        xuexingbt2.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                setting_xuexing.setText("AB");
                yesorno_xuexing.dismiss();
                ll_yesorno_xuexing.clearAnimation();
            }
        });
        xuexingbt3.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                setting_xuexing.setText("O");
                yesorno_xuexing.dismiss();
                ll_yesorno_xuexing.clearAnimation();
            }
        });

        // 过敏史
//        yesorno_guomin = new PopupWindow(Setting_self.this);
//        View view_guomin = getLayoutInflater().inflate(R.layout.item_yesorno, null);
//        ll_yesorno_guomin = (LinearLayout) view_guomin.findViewById(R.id.ll_yesorno);
//
//        yesorno_guomin.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
//        yesorno_guomin.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
//        yesorno_guomin.setBackgroundDrawable(new BitmapDrawable());
//        yesorno_guomin.setFocusable(true);
//        yesorno_guomin.setOutsideTouchable(true);
//        yesorno_guomin.setContentView(view_guomin);
//
//        RelativeLayout parent_guomin = (RelativeLayout) view_guomin.findViewById(R.id.parent);
//        final Button guominbt0 = (Button) view_guomin
//                .findViewById(R.id.item_yesorno_mid);
//        final Button guominbt1 = (Button) view_guomin
//                .findViewById(R.id.item_yesorno_yes);
//        final Button guominbt2 = (Button) view_guomin
//                .findViewById(R.id.item_yesorno_no);
//        final Button guominbt3 = (Button) view_guomin
//                .findViewById(R.id.item_yesorno_cancle);
//        parent_guomin.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                // TODO Auto-generated method stub
//                yesorno_guomin.dismiss();
//                ll_yesorno_guomin.clearAnimation();
//                guominbt0.setVisibility(View.GONE);
//            }
//        });
//
//        guominbt1.setOnClickListener(new View.OnClickListener() {
//            public void onClick(View v) {
//                setting_guomin.setText("是");
//                yesorno_guomin.dismiss();
//                ll_yesorno_guomin.clearAnimation();
//            }
//        });
//        guominbt2.setOnClickListener(new View.OnClickListener() {
//            public void onClick(View v) {
//                setting_guomin.setText("否");
//                yesorno_guomin.dismiss();
//                ll_yesorno_guomin.clearAnimation();
//            }
//        });
//        guominbt3.setOnClickListener(new View.OnClickListener() {
//            public void onClick(View v) {
//                yesorno_guomin.dismiss();
//                ll_yesorno_guomin.clearAnimation();
//            }
//        });
    }
}